// File Name: RotatePatternClientWrapper.cpp

#include "StdAfx.h"
#include "RotatePatternClientWrapper.h"

namespace UIAutomationCPP
{
	RotatePatternClientWrapper::RotatePatternClientWrapper(IUIAutomationPatternInstance *pInstance) : _pInstance(pInstance)
    {
        (*_pInstance).AddRef();
    }
    
    RotatePatternClientWrapper::~RotatePatternClientWrapper()
    {
        (*_pInstance).Release();
    }

	STDMETHODIMP RotatePatternClientWrapper::get_CurrentIsReadOnly(BOOL *pIsReadOnly)
	{
		return (*_pInstance).GetProperty(Rotation_GetIsReadOnly, FALSE, UIAutomationType_Bool, pIsReadOnly);
	}

	STDMETHODIMP RotatePatternClientWrapper::get_CachedIsReadOnly(BOOL *pIsReadOnly)
	{
		return (*_pInstance).GetProperty(Rotation_GetIsReadOnly, TRUE, UIAutomationType_Bool, pIsReadOnly);
	}

	STDMETHODIMP RotatePatternClientWrapper::get_CurrentValue(BSTR *pValue)
	{
		return (*_pInstance).GetProperty(Rotation_GetValue, FALSE, UIAutomationType_String, pValue);
	}

	STDMETHODIMP RotatePatternClientWrapper::get_CachedValue(BSTR *pValue)
	{
		return (*_pInstance).GetProperty(Rotation_GetValue, TRUE, UIAutomationType_String, pValue);
	}

	STDMETHODIMP RotatePatternClientWrapper::SetValue(LPCWSTR pValue)
	{
		UIAutomationParameter SetValueParams[] = { UIAutomationType_String, &pValue };
        return (*_pInstance).CallMethod(Rotation_SetValue,  SetValueParams, 
                (sizeof SetValueParams)/sizeof(UIAutomationParameter));
	}

	STDMETHODIMP RotatePatternClientWrapper::Reset()
	{
		return (*_pInstance).CallMethod(Rotation_Reset, NULL, 0);
	}

	HRESULT __stdcall RotatePatternClientWrapper::QueryInterface(REFIID riid, void **ppvObj)//riid == IID_IUIAutomationRegistrar, ppvObj == interface pointer to registrar
	{
		if(riid == IID_IUnknown)
			*ppvObj =(IUnknown*)this;
		else
		{
			*ppvObj = NULL;
			return E_NOINTERFACE;
		}

		((IUnknown*)(*ppvObj))->AddRef();
		return S_OK;
	}

	ULONG __stdcall RotatePatternClientWrapper::AddRef()
	{
		InterlockedIncrement(&refCount);
		return refCount;
	}

	ULONG __stdcall RotatePatternClientWrapper::Release()
	{
		ULONG ulRefCount = InterlockedDecrement(&refCount);
		if (ulRefCount == 0)
		{
			delete this;
		}
		return ulRefCount;
	}
}
